{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-14T01:49:35.614623Z",
     "start_time": "2025-10-14T01:49:35.594627Z"
    }
   },
   "source": [
    "# series的创建\n",
    "import pandas as pd\n",
    "s = pd.Series([10,2,3,4,5])\n",
    "print(s)\n",
    "# 自定义索引\n",
    "s = pd.Series([10,2,3,4,5],index=[\"A\",\"B\",\"C\",\"D\",\"E\"])\n",
    "print(s)\n",
    "# 定义name\n",
    "s = pd.Series([10,2,3,4,5],index=[\"A\",\"B\",\"C\",\"D\",\"E\"],name='月份')\n",
    "print(s)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1     2\n",
      "2     3\n",
      "3     4\n",
      "4     5\n",
      "dtype: int64\n",
      "A    10\n",
      "B     2\n",
      "C     3\n",
      "D     4\n",
      "E     5\n",
      "dtype: int64\n",
      "A    10\n",
      "B     2\n",
      "C     3\n",
      "D     4\n",
      "E     5\n",
      "Name: 月份, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T13:19:07.786847Z",
     "start_time": "2025-10-13T13:19:07.780338Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过字典来创建\n",
    "s = pd.Series({'a':1,\"b\":2,\"c\":3,'d':4,'e':5})\n",
    "print(s)\n",
    "s1 = pd.Series(s,index=['a','c'])\n",
    "print(s1)\n",
    "s2 = pd.Series()"
   ],
   "id": "c20bb42a888117df",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "e    5\n",
      "dtype: int64\n",
      "a    1\n",
      "c    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T13:56:30.135818Z",
     "start_time": "2025-10-13T13:56:30.119114Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# series的属性\n",
    "'''\n",
    "index:Series的索引对象\n",
    "values:Series的值\n",
    "dtype或dtypes：Series的元素类型\n",
    "shape:Series的形状\n",
    "ndim:Series的维度\n",
    "size:Series的元素个数\n",
    "name：Series的名称\n",
    "loc[]   显示索引，按标签索引或切片\n",
    "iloc[]  隐式索引，按位置索引或切片\n",
    "at[]  使用标签访问单个元素\n",
    "iat[]  使用位置访问单个元素\n",
    "\n",
    "'''\n",
    "\n",
    "# print(s.index)\n",
    "# print(s.values)\n",
    "# print(s.shape)\n",
    "# print(s.ndim)\n",
    "# print(s.size)\n",
    "# s.name = 'test'\n",
    "# print(s.dtype,s.name)\n",
    "print(s.loc['a':'c'])#显式索引 支持切片\n",
    "print(s.iloc[1]) # 隐式索引\n",
    "print(s.at['a']) # 不支持切片\n",
    "print(s.iat[0])"
   ],
   "id": "719fdbafc72510e8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "Name: test, dtype: int64\n",
      "2\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:13:33.127263Z",
     "start_time": "2025-10-13T14:13:33.106925Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 访问数据\n",
    "# print(s[0])\n",
    "# print(s['c'])\n",
    "# print(s[s<3])\n",
    "s['f'] = 6\n",
    "print(s.head(2))\n",
    "print(s.tail(1))"
   ],
   "id": "1c16633b5034c4f0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    10\n",
      "B     2\n",
      "Name: 月份, dtype: int64\n",
      "f    6\n",
      "Name: 月份, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:16:07.203050Z",
     "start_time": "2025-10-13T14:16:07.189231Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 常见函数\n",
    "import numpy as np\n",
    "s = pd.Series([10,2,np.nan,None,3,4,5], index=['A','B','C','D','E','F','G'], name='data')\n",
    "print(s)"
   ],
   "id": "6492d2a9ace1e160",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    10.0\n",
      "B     2.0\n",
      "C     NaN\n",
      "D     NaN\n",
      "E     3.0\n",
      "F     4.0\n",
      "G     5.0\n",
      "Name: data, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:17:18.224842Z",
     "start_time": "2025-10-13T14:17:18.205113Z"
    }
   },
   "cell_type": "code",
   "source": [
    "s.head(3) # 默认取前五行数据\n",
    "s.tail(2) # 默认取后五行数据"
   ],
   "id": "ad14300d0b0929e4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "F    4.0\n",
       "G    5.0\n",
       "Name: data, dtype: float64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:17:53.993050Z",
     "start_time": "2025-10-13T14:17:53.976546Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看所有描述信息\n",
    "s.describe()"
   ],
   "id": "13ba39107754bb8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     5.000000\n",
       "mean      4.800000\n",
       "std       3.114482\n",
       "min       2.000000\n",
       "25%       3.000000\n",
       "50%       4.000000\n",
       "75%       5.000000\n",
       "max      10.000000\n",
       "Name: data, dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:19:35.196064Z",
     "start_time": "2025-10-13T14:19:35.188494Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取元素个数(忽略缺失值)\n",
    "print(s.count())"
   ],
   "id": "cb5b0951629c6516",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:20:43.736439Z",
     "start_time": "2025-10-13T14:20:43.725734Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取索引\n",
    "print(s.keys()) #方法\n",
    "print(s.index) #属性"
   ],
   "id": "f02a6bb871b28f55",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['A', 'B', 'C', 'D', 'E', 'F', 'G'], dtype='object')\n",
      "Index(['A', 'B', 'C', 'D', 'E', 'F', 'G'], dtype='object')\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:22:02.504844Z",
     "start_time": "2025-10-13T14:22:02.491811Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(s.isna()) # 检查Series里的每一个元素是否是缺失值\n",
    "s.isna()"
   ],
   "id": "48f5a45b98578d94",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    False\n",
      "B    False\n",
      "C     True\n",
      "D     True\n",
      "E    False\n",
      "F    False\n",
      "G    False\n",
      "Name: data, dtype: bool\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "A    False\n",
       "B    False\n",
       "C     True\n",
       "D     True\n",
       "E    False\n",
       "F    False\n",
       "G    False\n",
       "Name: data, dtype: bool"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:23:13.054128Z",
     "start_time": "2025-10-13T14:23:13.039859Z"
    }
   },
   "cell_type": "code",
   "source": "s.isin([4,5,6]) # 检查每个元素是否在参数集合中",
   "id": "15cb28cd2cb68437",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    False\n",
       "B    False\n",
       "C    False\n",
       "D    False\n",
       "E    False\n",
       "F     True\n",
       "G     True\n",
       "Name: data, dtype: bool"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:24:17.155679Z",
     "start_time": "2025-10-13T14:24:17.140028Z"
    }
   },
   "cell_type": "code",
   "source": "s.describe()",
   "id": "6c24335cda9f9c0f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     5.000000\n",
       "mean      4.800000\n",
       "std       3.114482\n",
       "min       2.000000\n",
       "25%       3.000000\n",
       "50%       4.000000\n",
       "75%       5.000000\n",
       "max      10.000000\n",
       "Name: data, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:27:02.192048Z",
     "start_time": "2025-10-13T14:27:02.183609Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(s.mean()) # 平均值\n",
    "print(s.sum()) # 总和\n",
    "print(s.std()) # 标准差\n",
    "print(s.var()) # 方差\n",
    "print(s.min()) # 最小值\n",
    "print(s.max()) # 最大值\n",
    "print(s.median()) # 中位数\n"
   ],
   "id": "c41d890d6a025739",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.8\n",
      "24.0\n",
      "3.1144823004794877\n",
      "9.700000000000001\n",
      "2.0\n",
      "10.0\n",
      "4.0\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:27:19.275533Z",
     "start_time": "2025-10-13T14:27:19.256708Z"
    }
   },
   "cell_type": "code",
   "source": "print(s)",
   "id": "56ae4136fc1035a4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    10.0\n",
      "B     2.0\n",
      "C     NaN\n",
      "D     NaN\n",
      "E     3.0\n",
      "F     4.0\n",
      "G     5.0\n",
      "Name: data, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:28:53.913001Z",
     "start_time": "2025-10-13T14:28:53.906357Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(s.sort_values())\n",
    "print(s.quantile(0.25)) # 分位数"
   ],
   "id": "603fa9a713e9a8a4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B     2.0\n",
      "E     3.0\n",
      "F     4.0\n",
      "G     5.0\n",
      "A    10.0\n",
      "C     NaN\n",
      "D     NaN\n",
      "Name: data, dtype: float64\n",
      "3.0\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:32:10.239779Z",
     "start_time": "2025-10-13T14:32:10.227903Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 众数\n",
    "s['H'] = 4\n",
    "print(s.mode())"
   ],
   "id": "e2f5f4b8a07e69ea",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    4.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:32:40.907642Z",
     "start_time": "2025-10-13T14:32:40.900940Z"
    }
   },
   "cell_type": "code",
   "source": "print(s.value_counts()) # 每个元素的计数",
   "id": "760f38930af6a3d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.0     2\n",
      "10.0    1\n",
      "2.0     1\n",
      "3.0     1\n",
      "5.0     1\n",
      "Name: data, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T02:43:06.286299Z",
     "start_time": "2025-10-14T02:43:06.279317Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 去重\n",
    "s.drop_duplicates()\n",
    "s.unique()\n",
    "print(s.nunique())"
   ],
   "id": "f7058e928332cf21",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:36:40.191096Z",
     "start_time": "2025-10-13T14:36:40.185473Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 排序 值、索引\n",
    "print(s.sort_index()) # 按索引排序\n",
    "print(s.sort_values()) # 按值排序"
   ],
   "id": "94879c650a6561da",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    10.0\n",
      "B     2.0\n",
      "C     NaN\n",
      "D     NaN\n",
      "E     3.0\n",
      "F     4.0\n",
      "G     5.0\n",
      "H     4.0\n",
      "Name: data, dtype: float64\n",
      "B     2.0\n",
      "E     3.0\n",
      "F     4.0\n",
      "H     4.0\n",
      "G     5.0\n",
      "A    10.0\n",
      "C     NaN\n",
      "D     NaN\n",
      "Name: data, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "创建一个包含10名学生数学成绩的Series，成绩范围在50-100之间。\n",
    "计算平均分、最高分、最低分，并找出高于平均分的学生人数。\n",
    "'''"
   ],
   "id": "a192f23a9f6a2f3b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:51:25.287832Z",
     "start_time": "2025-10-13T14:51:25.267469Z"
    }
   },
   "cell_type": "code",
   "source": [
    "np.random.seed(42)\n",
    "scores = pd.Series(np.random.randint(50,101,10),index=['学生'+str(i)for i in range(1,11)])\n",
    "print(scores)\n",
    "print('平均分：',scores.mean())\n",
    "print('最高分：',scores.max())\n",
    "print('最低分：',scores.min())\n",
    "# 高于平均分的学生人数\n",
    "mean = scores.mean()\n",
    "print('高于平均分的学生人数：',len(scores[scores>mean]))\n",
    "print('高于平均分的学生人数：',scores[scores>mean].count())"
   ],
   "id": "c1ed21affb011ac8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学生1     88\n",
      "学生2     78\n",
      "学生3     64\n",
      "学生4     92\n",
      "学生5     57\n",
      "学生6     70\n",
      "学生7     88\n",
      "学生8     68\n",
      "学生9     72\n",
      "学生10    60\n",
      "dtype: int32\n",
      "平均分： 73.7\n",
      "最高分： 92\n",
      "最低分： 57\n",
      "高于平均分的学生人数： 4\n",
      "高于平均分的学生人数： 4\n"
     ]
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "给定某城市一周每天的最高温度Series，完成一下任务：\n",
    "- 找出温度超过30度的天数\n",
    "- 计算平均温度\n",
    "- 将温度从高到底排序\n",
    "- 找出温度变化最大的两天\n",
    "'''"
   ],
   "id": "1782fc2b5df03cd9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:56:06.598700Z",
     "start_time": "2025-10-13T14:56:06.582983Z"
    }
   },
   "cell_type": "code",
   "source": "temperatures = pd.Series([28,31,29,32,30,27,33],index=['周一','周二','周三','周四','周五','周六','周日'])",
   "id": "70582baa6d725501",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T14:59:15.337319Z",
     "start_time": "2025-10-13T14:59:15.321991Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 找出温度超过30度的天数\n",
    "print('超过30度的天数：',len(temperatures[temperatures>30]))"
   ],
   "id": "3f96c541f25dc9cc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 3\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T15:00:13.612994Z",
     "start_time": "2025-10-13T15:00:13.604451Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算平均温度\n",
    "print('平均温度：',temperatures.mean())"
   ],
   "id": "a122a4556c29a7ab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均温度： 30.0\n"
     ]
    }
   ],
   "execution_count": 58
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T15:07:27.285627Z",
     "start_time": "2025-10-13T15:07:27.269685Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 温度从高到低排序\n",
    "print('温度从高到低排序：',temperatures.sort_values(ascending=False))"
   ],
   "id": "1596d7f34e209a8f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "温度从高到低排序： 周日    33\n",
      "周四    32\n",
      "周二    31\n",
      "周五    30\n",
      "周三    29\n",
      "周一    28\n",
      "周六    27\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T15:12:50.381225Z",
     "start_time": "2025-10-13T15:12:50.366893Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 找出温度变化最大的两天\n",
    "# 28,31,29,32,30,27,33\n",
    "# none,3,-2,3,-2,-3,6\n",
    "t3 = temperatures.diff().abs() # 计算series的变化值\n",
    "t3.sort_values(ascending=False) .keys()\n",
    "print('温度变化最大的两天：',*(t3.sort_values(ascending=False) .keys()[:2].tolist()))"
   ],
   "id": "14f7184eee37d16f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "温度变化最大的两天： 周日 周二\n"
     ]
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "给定某股票连续10个交易日的收盘价Series:\n",
    "- 计算每日收益率（当日收盘价/前日收盘价-1）\n",
    "- 找出收益率最高和最低的日期\n",
    "- 计算波动率（收益率的标准差）\n",
    "'''"
   ],
   "id": "14420d3b2c6dde03"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T02:42:21.156933Z",
     "start_time": "2025-10-14T02:42:21.132944Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "# 日期序列\n",
    "# date = pd.date_range('2025-06-1',periods=6)\n",
    "# print(list(date))\n",
    "prices = pd.Series([102.3,103.5,105.1,104.8,106.2,107.0,106.5,108.1,109.3,110.2], index=pd.date_range('2023-01-01',periods=10))\n",
    "print('每日收益率：',prices.pct_change()) # percent\n",
    "a = prices.pct_change()\n",
    "print('收益率最高的日期：',a.idxmax())\n",
    "print('收益率最低的日期：',a.idxmin())\n",
    "print('波动率：',a.std())"
   ],
   "id": "22b63953cde3e716",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每日收益率： 2023-01-01         NaN\n",
      "2023-01-02    0.011730\n",
      "2023-01-03    0.015459\n",
      "2023-01-04   -0.002854\n",
      "2023-01-05    0.013359\n",
      "2023-01-06    0.007533\n",
      "2023-01-07   -0.004673\n",
      "2023-01-08    0.015023\n",
      "2023-01-09    0.011101\n",
      "2023-01-10    0.008234\n",
      "Freq: D, dtype: float64\n",
      "收益率最高的日期： 2023-01-03 00:00:00\n",
      "收益率最低的日期： 2023-01-07 00:00:00\n",
      "波动率： 0.007373623845361105\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "某产品过去12个月的销售量Series：\n",
    "- 计算季度平均销量（每三个月为一个季度）\n",
    "- 找出销量最高的月份\n",
    "- 计算月环比增长率\n",
    "- 找出连续增长超过2 个月的月份\n",
    "'''"
   ],
   "id": "af75ed5f572fa72d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T02:53:43.798275Z",
     "start_time": "2025-10-14T02:53:43.777330Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sales = pd.Series([120,135,145,160,155,170,180,175,190,200,210,220],index=pd.date_range('2022-01-01',periods=12,freq='MS'))\n",
    "print(sales)"
   ],
   "id": "572fa9813bb89357",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-01-01    120\n",
      "2022-02-01    135\n",
      "2022-03-01    145\n",
      "2022-04-01    160\n",
      "2022-05-01    155\n",
      "2022-06-01    170\n",
      "2022-07-01    180\n",
      "2022-08-01    175\n",
      "2022-09-01    190\n",
      "2022-10-01    200\n",
      "2022-11-01    210\n",
      "2022-12-01    220\n",
      "Freq: MS, dtype: int64\n",
      "季度平均销量：\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T02:55:15.855879Z",
     "start_time": "2025-10-14T02:55:15.837882Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 季度的平均销量\n",
    "sales.resample('QS').mean() # 重新采样"
   ],
   "id": "5dee43db5939f616",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2022-01-01    133.333333\n",
       "2022-04-01    161.666667\n",
       "2022-07-01    181.666667\n",
       "2022-10-01    210.000000\n",
       "Freq: QS-JAN, dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T03:06:01.961133Z",
     "start_time": "2025-10-14T03:06:01.944640Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 销量最高的月份\n",
    "print('销量最高的月份：',sales.idxmax())\n",
    "print(sales.pct_change()) # 月环比增长率\n",
    "#连续增长超过两个月的月份\n",
    "a = sales.pct_change()\n",
    "b = a>0\n",
    "b[b.rolling(3).sum() == 3].keys().tolist()"
   ],
   "id": "7c5f08ec43207871",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "销量最高的月份： 2022-12-01 00:00:00\n",
      "2022-01-01         NaN\n",
      "2022-02-01    0.125000\n",
      "2022-03-01    0.074074\n",
      "2022-04-01    0.103448\n",
      "2022-05-01   -0.031250\n",
      "2022-06-01    0.096774\n",
      "2022-07-01    0.058824\n",
      "2022-08-01   -0.027778\n",
      "2022-09-01    0.085714\n",
      "2022-10-01    0.052632\n",
      "2022-11-01    0.050000\n",
      "2022-12-01    0.047619\n",
      "Freq: MS, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[Timestamp('2022-04-01 00:00:00'),\n",
       " Timestamp('2022-11-01 00:00:00'),\n",
       " Timestamp('2022-12-01 00:00:00')]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "某商店每小时销售额Series:\n",
    "- 按天重采样计算每日总销售额\n",
    "- 计算每天营业时间（8：00-22：00）和非营业时间的销售额比例\n",
    "- 找出销售额最高的三个小时\n",
    "'''"
   ],
   "id": "bd40cc0520d6234d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T03:26:35.464526Z",
     "start_time": "2025-10-14T03:26:35.439362Z"
    }
   },
   "cell_type": "code",
   "source": [
    "np.random.seed(42)\n",
    "hourly_sales = pd.Series(np.random.randint(0,100,24),\n",
    "                         index = pd.date_range('2025-01-01',periods=24,freq='H')\n",
    "                         )\n",
    "# 按天重采样计算每日总销售额\n",
    "day_sales = hourly_sales.resample('D').sum()\n",
    "# hourly_sales.sum()\n",
    "# 计算每天营业时间（8：00-22：00）和非营业时间的销售额比例\n",
    "# hourly_sales.between_time('8:00','22:00') # 筛选一段时间内的Series\n",
    "# business_hours_sales = hourly_sales[(hourly_sales.index.hour>=8) & (hourly_sales.index.hour<=22)].sum()\n",
    "# print(business_hours_sales / (day_sales - business_hours_sales))\n",
    "\n",
    "\n",
    "business_hours_sales = hourly_sales[(hourly_sales.index.hour>=8) & (hourly_sales.index.hour<=22)]\n",
    "not_business_hours_sales = hourly_sales.drop(business_hours_sales.index).sum()\n",
    "print(business_hours_sales.sum() / not_business_hours_sales)\n",
    "\n",
    "# 找出销售额最高的三个小时\n",
    "print(hourly_sales.nlargest(3).keys())"
   ],
   "id": "394094d2019ea413",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.4294354838709677\n",
      "DatetimeIndex(['2025-01-01 11:00:00', '2025-01-01 01:00:00',\n",
      "               '2025-01-01 10:00:00'],\n",
      "              dtype='datetime64[ns]', freq=None)\n"
     ]
    }
   ],
   "execution_count": 45
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
